<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 具体页面结构 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px ">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="addPart('')">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree :data="list" :default-expand-all="true">
          <template #default="{ data }">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px; width: 100%;"
            >
              <el-col :span="20">
                <span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <el-col>
                    <!-- 下拉菜单 element -->
                    <el-dropdown>
                      <span> 操作<i class="el-icon-arrow-down" /> </span>
                      <!-- 下拉菜单 -->
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="addPart(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="editPart(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-show="data.children.length===0" @click.native="delPart(data.id)">删除部门</el-dropdown-item>
                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
        <el-dialog
          :title="isEdit?'编辑部门':'添加部门'"
          :visible.sync="showDialog"
          :close-on-click-modal="false"
          @closed="hClose"
        >
          <dialog-form

            :id="curId"
            ref="dialogForm"
            :is-edit="isEdit"
            :origin-list="originList"
            @success="hSuccess"
            @close="showDialog=false"
          />
        </el-dialog>
      </el-card>

    </div>
  </div>
</template>
<script>
import { delDepartments, getDepartments } from '@/api/departments'
import { transDataToTreeData } from '@/utils'
import DialogForm from './DialogForm.vue'
export default {
  components: { DialogForm },
  data() {
    return {
      list: [],
      showDialog: false,
      form: {},
      curId: '',
      isEdit: false,
      originList: []
    }
  },
  created() {
    this.loadDepartments()
  },
  methods: {
    async  loadDepartments() {
      try {
        const res = await getDepartments()
        // console.log(res)
        if (res.code !== 10000) return this.$message.error(res.message)
        this.$message.success(res.message)
        res.data.depts.shift()
        this.originList = res.data.depts.map(item => {
          const { id, name, pid, code } = item
          return { id, name, pid, code }
        })
        this.list = transDataToTreeData(res.data.depts)
      } catch (error) {
        console.log(error)
      }
    },
    addPart(id) {
      this.isEdit = false
      this.curId = id
      this.showDialog = true
    },
    editPart(id) {
      this.isEdit = true
      this.curId = id
      this.showDialog = true
      this.$nextTick(() => {
        this.$refs.dialogForm.loadDepartmentDetail()
      })
    },
    delPart(id) {
      this.$confirm('确认删除此部门?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        const res = await delDepartments(id)
        if (res.code === 10000) {
          if (res.success) {
            this.$message.success(res.message)
            this.loadDepartments()
          }
        }
      }).catch((error) => {
        this.$message.error(error)
      })
    },
    hSuccess() {
      this.loadDepartments()
      this.showDialog = false
    },
    hClose() {
      this.$refs.dialogForm.resetForm()
    }
  }
}
</script>
